mui.init();

//倒计时  
var countdown=60;  
function settime(val) {  
	var num = parseInt(document.getElementById("phoneNum").value);
	check_mobile(num,val);
}

mui.plusReady(function () {
	var phoneNum = document.getElementById('phoneNum');
	var verCode = document.getElementById('verCode');
	var username = document.getElementById('username');    
	var password = document.getElementById('password');
	var register = document.getElementById('register');
   
   register.addEventListener('tap',function(){
		if(phoneNum.value.length == 0){
			plus.ui.toast('手机号不能为空');
			return false;
		}
		if(verCode.value.length == 0){
			plus.ui.toast('验证码不能为空');
			return false;
		}
		if(username.value.length == 0){
			plus.ui.toast('用户名不能为空');
			return false;
		}
		if(password.value.length == 0){
			plus.ui.toast('密码不能为空');
			return false;
		}
		
		var api_host = plus.storage.getItem('HOST_URL');
		var synlogininfo=plus.storage.getItem('synlogininfo');
		var synlogintype=plus.storage.getItem('synlogintype');
		var ws = plus.webview.currentWebview();
		mui.ajax({
			url:api_host+"/api/linxuan/user/newaccount",
			data:{'username':username.value,'password':password.value,'mobile':phoneNum.value,'vercode':verCode.value,'synlogininfo':synlogininfo,'synlogintype':synlogintype},
			dataType:'json',//服务器返回json格式数据
			type:'post',//HTTP请求类型
			headers:{'User-Token':plus.storage.getItem('UserToken')},
			timeout:3000,//超时时间设置为10秒；
			success:function(data){
				if(data.code==1){
					plus.storage.setItem('UserToken', data.data);
					mui.toast(data.msg,{ duration:'2000', type:'div'});
                    plus.webview.getWebviewById('createNewAccount').close();
					plus.webview.getWebviewById('loginHtml').close();
					plus.webview.getWebviewById('bindAccount').close();
					plus.webview.close(ws);
					
				}else{
					
					mui.toast(data.msg);
				}
			},
			error:function(xhr,type,errorThrown){
				mui.toast("请求失败，请检查网络！");
			}
		});
	});
   
});

//验证手机是否可以用
function check_mobile(num,val) {
    var api_host = plus.storage.getItem('HOST_URL');
    mui.ajax({
        url:api_host+'/api/linxuan/user/checkmobile',
        data:{'mobile':num,},
        dataType:'json',//服务器返回json格式数据
        type:'post',//HTTP请求类型
        timeout:2000,//超时时间设置为10秒；
        success:function(msg){
        	if(msg.code == 1){
                if (countdown == 0) {
                    clearInterval(in1);
                    val.removeAttribute("disabled");
                    val.value="获取验证码";
                    countdown = 60;
                    return false;
                } else {
                    var in1 = setInterval(function () {
                            val.setAttribute("disabled", true);
                            val.value= countdown + "s后重新发送";
                            countdown--;
                            if(countdown == 0){
                            	clearInterval(in1);
                            	val.removeAttribute("disabled");
			                    val.value="获取验证码";
			                    countdown = 60;
			                    return false;
                            }
                        },1000)
                }
                fs_code(num);
			}else{
                mui.toast(msg.msg,{ duration:'2000', type:'div'});
			}
        },
        error:function(xhr,type,errorThrown){
            mui.toast('检测手机号失败，请检查网络！',{ duration:'2000', type:'div'});
        }
    });
}

//发送验证码
function fs_code(num) {
    var api_host = plus.storage.getItem('HOST_URL');
    mui.ajax({
        url:api_host+'/api/linxuan/user/sendverify',
        data:{'account':num},
        dataType:'json',
        type:'post',
        timeout:10000,
        success:function(msg){
            if(msg.code == 1){
                mui.toast(msg.msg,{ duration:'1000', type:'div'});
            }else{
                mui.toast(msg.msg,{ duration:'1000', type:'div'});
            }
        },
        error:function(xhr,type,errorThrown){
            mui.toast('请求错误，请检查网络！',{ duration:'2000', type:'div'});
        }
    });
}